package pageTest.nologin;

import constants.HostConstant;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class PageByNoLogin extends WebDriverUtils {
    private final String indexUrl = "http://" + HostConstant.HOST + ":5173/";
    private final String teamUrl = "http://" + HostConstant.HOST + ":5173/team";
    private final String messageUrl = "http://" + HostConstant.HOST + ":5173/message";
    private final String userUrl = "http://" + HostConstant.HOST + ":5173/user";



    public PageByNoLogin() {
        super(INIT_URL);
    }


    @Test(priority = 1)
    public void indexPageByNoLogin() throws InterruptedException {
        String topNavBarCssSle = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        // 1. 验证页面可以正确加载
        driver.get(indexUrl);
        WebElement topNavBar = driver.findElement(By.cssSelector(topNavBarCssSle));
        assert "CAMPUS 伙伴匹配".equals(topNavBar.getText());
        // 2. 用户头像可点击，点击后进入用户详情页
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > a > div")));
        driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > a > div")).click();
        wait.until(ExpectedConditions.textToBePresentInElement(topNavBar, "用户详情"));
//        Thread.sleep(2000);
        driver.navigate().back();
        // 4. 私聊按钮点击后出现弹窗
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "CAMPUS 伙伴匹配"));
        driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div.van-card__bottom > div.van-card__num > button:nth-child(2)")).click();
        WebElement dialog = driver.findElement(By.cssSelector("body > div.van-popup.van-popup--center.van-dialog > div.van-dialog__content.van-dialog__content--isolated > div"));
        assert "该功能需要登陆后使用,是否登录".equals(dialog.getText());
        // 3. 关注按钮点击后跳转至用户登录页
        driver.findElement(By.cssSelector("body > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button.van-button.van-button--default.van-button--large.van-dialog__cancel")).click();
        driver.findElement(By.cssSelector("#van-tab-7 > div > div > div.van-list > div:nth-child(1) > div > div > div.van-card__bottom > div.van-card__num > button:nth-child(1)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "登录"));
        driver.navigate().back();
//        Thread.sleep(2000);
        // 5. 按标签匹配用户按钮可点击，跳转至用户登录页
        driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-cell.van-cell--center > div.van-switch")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "登录"));
//        Thread.sleep(2000);
        // 6. 在 index 页面点击 “队伍” 可跳转
        driver.navigate().back();
        driver.findElement(By.cssSelector("#app > div.van-tabbar.van-tabbar--fixed.van-hairline--top-bottom.van-safe-area-bottom > div:nth-child(2)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "队伍"));
        // 7. 在 index 页面点击 “消息” 或 “我的” 调整至登录页
        driver.navigate().back();
        driver.findElement(By.cssSelector("#app > div.van-tabbar.van-tabbar--fixed.van-hairline--top-bottom.van-safe-area-bottom > div:nth-child(3)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "登录"));
//        Thread.sleep(2000);
        driver.navigate().back();
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#app > div.van-tabbar.van-tabbar--fixed.van-hairline--top-bottom.van-safe-area-bottom > div:nth-child(4)"))).click();
//        driver.findElement(By.cssSelector("#app > div.van-tabbar.van-tabbar--fixed.van-hairline--top-bottom.van-safe-area-bottom > div:nth-child(4)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarCssSle), "登录"));
//        Thread.sleep(2000);

    }

    @Test(priority = 2)
    public void teamPageByNoLogin() {
        driver.get(teamUrl);
        // 1. 页面可以正常加载
        String topNavBarText = driver.findElement(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis")).getText();
        assert "队伍".equals(topNavBarText);
        try {
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1)")));
            // 2. 如果存在队伍，元素可找到且文本为 "加入队伍"
            String buttonText = driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1) > div.van-card__footer > button > div > span")).getText();
            assert "加入队伍".equals(buttonText);
            // 3. 点击按钮后跳转至登录页
            driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div:nth-child(1) > div.van-card__footer > button")).click();
            wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "登录"));
            driver.navigate().back();
        } catch (Exception e) {
            log.info("当前页面没有队伍");
        }
        // 4. 点击创建队伍跳转到登录页面
        driver.findElement(By.cssSelector("#teamPage > button")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "登录"));
        // TODO 这里元素定位有问题
        // 5. 点击“加密”按钮，若存在队伍，则出现弹窗且输入密码后跳转至登录页面
        driver.navigate().back();
        driver.findElement(By.cssSelector("#van-tabs-3-1")).click();
        try {
            driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div.van-card > div.van-card__footer > button")).click();
            String cueText = driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div.van-popup.van-popup--center.van-dialog > div.van-dialog__header")).getText();
            assert "请输入密码".equals(cueText);
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button.van-button.van-button--default.van-button--large.van-dialog__cancel")));
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button.van-button.van-button--default.van-button--large.van-dialog__confirm.van-hairline--left")));
            // 输入密码后点击“加入”,页面跳转
            driver.findElement(By.cssSelector("#van-field-7-input")).sendKeys("231asd65as4d");
            driver.findElement(By.cssSelector("#teamPage > div.van-pull-refresh > div > div.van-list > div.van-popup.van-popup--center.van-dialog > div.van-hairline--top.van-dialog__footer > button.van-button.van-button--default.van-button--large.van-dialog__confirm.van-hairline--left")).click();
            wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "登录"));
        } catch (Exception e) {
            log.info("不存在加密队伍");
        }
    }

    @Test(priority = 3)
    public void canSearch() {
        // 1. 主页用户搜索可用
        // 1) 搜索的用户存在，用户名中包含输入的关键字
        driver.get(indexUrl);
        Actions actions = new Actions(driver);
        String keyword = "h";
        WebElement input = driver.findElement(By.tagName("input"));
        input.sendKeys(keyword);
        actions.sendKeys(input,Keys.ENTER).perform();
        // 获取用户名并检查
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div:nth-child(1) > div.van-card__title.van-multi-ellipsis--l2")));
        String username = driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div:nth-child(1) > div.van-card__title.van-multi-ellipsis--l2")).getText();
        assert username.contains(keyword);
        // 2) 若搜索的用户不存在，则元素不存在
        keyword = "啦啦啦0321346";
        inputClearAndSend(input, keyword);
        actions.sendKeys(input,Keys.ENTER).perform();
        try {
            driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div:nth-child(1) > div.van-card__title.van-multi-ellipsis--l2"));
        } catch (Exception e) {
            log.info("结果符合预期，找不到元素");
        }

        // 2. 队伍页面搜索可用
        driver.get(teamUrl);
        input = driver.findElement(By.tagName("input"));
        keyword = "666";
        inputClearAndSend(input, keyword);
        actions.sendKeys(input, Keys.ENTER).perform();
        try {
            String cls = "#teamPage > div.van-pull-refresh > div > div.van-list > div.van-card > div.van-card__header > div > div:nth-child(1) > div";
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("van-card")));
            assert driver.findElement(By.cssSelector(cls)).getText().contains(keyword);
        } catch (Exception e) {
            log.info("搜索队伍不存在");
        }
        // 3. 在任意页面都可以按照标签搜索用户
        List<String> pageUrlList = Arrays.asList(indexUrl, teamUrl, messageUrl);
        for (String url : pageUrlList) {
            driver.get(url);
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__right.van-haptics-feedback")));
            driver.findElement(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__right.van-haptics-feedback")).click();
            WebElement tagButton = driver.findElement(By.cssSelector("#searchPage > form > div.van-tree-select > div.van-tree-select__content > div:nth-child(1)"));
            String tagName = tagButton.getText();
            tagButton.click();
            WebElement searchButton = driver.findElement(By.cssSelector("#searchPage > div > button"));
            driver.manage().window().maximize();
            actions.scrollByAmount(0, 1200).perform();
            searchButton.click();
            try {
                wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("van-card")));
            } catch (Exception e) {
                log.info("找不到相关标签的用户");
                continue;
            }
            List<WebElement> elements = driver.findElements(By.tagName("span"));
            boolean exist = false;
            for (WebElement element : elements) {
                if (element.getText().equals(tagName)) {
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                throw new RuntimeException("按标签搜索用户成功，但用户标签不符合预期");
            }
        }
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
